/*
 * @Description: 
 * @Version: 1.0
 * @Author: wenbin
 * @Date: 2021-03-23 13:42:45
 * @LastEditors: wenbin
 * @LastEditTime: 2021-08-18 16:58:06
 * @FilePath: /magus-plugin/vue.config.js
 * Copyright (C) 2021 wenbin. All rights reserved.
 */

const path = require('path');
const CompressionWebpackPlugin = require("compression-webpack-plugin"); // 开启gzip压缩， 按需引用
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; // 开启gzip压缩， 按需写入
const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);


const resolve = (dir) => path.join(__dirname, dir);

module.exports = {
  lintOnSave: !IS_PROD,
  pages: {
    index: {
      entry: 'examples/main.js',
      template: 'public/index.html',
      filename: 'index.html'
    }
  },
  chainWebpack: config => {
    config.resolve.alias // 添加别名
      .set('@', resolve('packages'))
      .set('@examples', resolve('examples'))
      .set('@resource', resolve('resource'))
      .set('@assets', resolve('examples/assets'))
      .set('@components', resolve('examples/components'))
      .set('@views', resolve('examples/views'))
      .set('@store', resolve('examples/store'));
      if (IS_PROD) {
        // 压缩图片˜
        config.module.rule('images')
          .test(/\.(png|jpe?g|gif|webp)(\?.*)?$/)
          .use('image-webpack-loader')
          .loader('image-webpack-loader')
          .options({
            bypassOnDebug: true
          })
          .end()
          .use('url-loader')
          .loader('file-loader')
          .options({
            name: 'assets/[name].[hash:8].[ext]'
          }).end()
        config.module.rule('svg')
          .test(/\.(svg)(\?.*)?$/)
          .use('file-loader')
          .loader('file-loader')
          .options({
            name: 'assets/[name].[hash:8].[ext]'
          });
      }
  },
  // configureWebpack: {
  //   output: {
  //     libraryExport: 'default'
  //   }
  // },
  configureWebpack: config => {
    // 公共代码抽离
    config.optimization = {
      // 分割代码块
      splitChunks: {
        cacheGroups: {
          //公用模块抽离
          common: {
            chunks: 'initial',
            minSize: 0, //大于0个字节
            minChunks: 2, //抽离公共代码时，这个代码块最小被引用的次数
          },
          //第三方库抽离
          vendor: {
            priority: 1, //权重
            test: /node_modules/,
            chunks: 'initial',
            minSize: 0, //大于0个字节
            minChunks: 2, //在分割之前，这个代码块最小应该被引用的次数
          },
        },
      }
    };
    // 开启 gzip 压缩
    const plugins = [];
    if (IS_PROD) {
      plugins.push(
        new CompressionWebpackPlugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8
        })
      );
    }
    config.plugins = [...config.plugins, ...plugins];
  },
  css: {
    extract: IS_PROD,
  },
  devServer: {
    overlay: { // 让浏览器 overlay 同时显示警告和错误
      warnings: true,
      errors: true
    },
    host: "localhost",
    port: 8088, // 端口号
    https: false, // https:{type:Boolean}
    open: 'Google Chrome', //配置自动启动浏览器
    hotOnly: true, // 热更新
    proxy: {
      '/api/mboxms/*': {
        target: 'http://192.168.30.11:33000', //网关
        changeOrigin: true, //跨域
        pathRewrite: {
          "^/api/mboxms": ""
        }
      },
      '/api/mlinkClient/*': {
        target: 'http://192.168.30.11:20000', //网关
        changeOrigin: true, //跨域
        pathRewrite: {
          "^/api/mlinkClient": ""
        }
      },
      '/api/platform/*': {
        target: 'http://192.168.20.31:12000', //网关
        changeOrigin: true, //跨域
        pathRewrite: {
          "^/api/platform": ""
        }
      },
      '/api/authCenter/*': {
        target: 'http://192.168.30.11:13000', //网关
        changeOrigin: true, //跨域
        pathRewrite: {
          "^/api/authCenter": ""
        }
      },
      '/api/equipment/*': {
        target: 'http://47.100.177.241:30000/', //网关
        changeOrigin: true, //跨域
        pathRewrite: {
          "^/api/equipment": ""
        }
      },
      '/api/appStoreAdmin/*': {
        target: 'http://192.168.20.31:21001', //网关
        changeOrigin: true, //跨域
        pathRewrite: {
          "^/api/appStoreAdmin": ""
        }
      },
    }
  },
  productionSourceMap: false,
}